package com.waimai.mobile.ui.adapter;

import android.content.Context;
import android.media.Rating;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.waimai.mobile.R;
import com.waimai.mobile.api.entity.Comment;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lin on 2015/6/14.
 */

@EBean
public class CommentItemAdpater extends BaseAdapter {

    @RootContext
    public Context context;

    public List<Comment> comments;


    @Override
    public int getCount() {
        return comments.size();
    }

    @Override
    public Object getItem(int position) {
        return comments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_comment, null);
            holder.userNameTv = (TextView) convertView.findViewById(R.id.userNameTv);
            holder.starLevelRb = (RatingBar) convertView.findViewById(R.id.starLevelRb);
            holder.starLevelTv = (TextView) convertView.findViewById(R.id.starLevelTv);
            holder.contentTv = (TextView) convertView.findViewById(R.id.contentTv);
            holder.commentTimeTv = (TextView) convertView.findViewById(R.id.commentTimeTv);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String putTimeStr = format.format(comments.get(position).getPubTime());
        holder.commentTimeTv.setText(putTimeStr);

        holder.userNameTv.setText(comments.get(position).getUser().getUserName());
        holder.starLevelTv.setText(comments.get(position).getStarLevel()+"");
        holder.starLevelRb.setRating(comments.get(position).getStarLevel());
        if(comments.get(position).getContent() != null && !"".equals(comments.get(position).getContent())) {
            holder.contentTv.setText(comments.get(position).getContent());
            holder.contentTv.setVisibility(View.VISIBLE);
        }else{
            holder.contentTv.setVisibility(View.GONE);
        }

        return convertView;
    }

    public class ViewHolder{
        TextView userNameTv;
        TextView starLevelTv;
        RatingBar starLevelRb;
        TextView contentTv;
        TextView commentTimeTv;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }
}
